﻿@model DiplomaWeb.Models.CompaniesAndLecturers.LecturerPracticeViewModel

<script type="text/javascript" language="javascript">
    function UpdateLecturerLoadingForCompany(lecturerId, levelId) {
        var value = $("#lecturer_Loading_" + levelId).val();
        var path = '@Url.RouteUrl("Default", new {controller = "SystemManagers", action = "UpdateLecturerLoading"})';
        $.ajax({
            url: path,
            type: "POST",
            async: false,
            data: ({ lecturerId: lecturerId, levelId: levelId, value: value }),
            success: function () {
                GetLecturerPracticeDetails(lecturerId);
            }
        });
    }

    function AddCompanyForLecturer(lecturerId, levelId) {
        var companyId = $("#selectListCompanies_level_" + levelId).val();
        var path = '@Url.RouteUrl("Default", new {controller = "SystemManagers", action = "AddCompanyForLecturer"})';
        $.ajax({
            url: path,
            type: "POST",
            async: false,
            data: ({ lecturerId: lecturerId, levelId: levelId, companyId: companyId }),
            success: function () {
                GetLecturerPracticeDetails(lecturerId);
            }
        });
    }

    function DeleteCompanyFromLecturer(lecturerId, levelId, companyId) {
        var path = '@Url.RouteUrl("Default", new {controller = "SystemManagers", action = "DeleteCompanyFromLecturer"})';
        $.ajax({
            url: path,
            type: "POST",
            async: false,
            data: ({ lecturerId: lecturerId, levelId: levelId, companyId: companyId }),
            success: function () {
                GetLecturerPracticeDetails(lecturerId);
            }
        });
    }

    function AddStudentForLecturer(lecturerId) {
        var studentId = $("#selectListStudents").val();
        var path = '@Url.RouteUrl("Default", new {controller = "SystemManagers", action = "AddStudentForLecturer"})';
        $.ajax({
            url: path,
            type: "POST",
            async: false,
            data: ({ studentId: studentId, lecturerId: lecturerId }),
            success: function () {
                GetLecturerPracticeDetails(lecturerId);
            }
        });
    }

    function DeleteStudentFromLecturer(studentId) {
        var path = '@Url.RouteUrl("Default", new {controller = "SystemManagers", action = "DeleteStudentFromLecturer"})';
        $.ajax({
            url: path,
            type: "POST",
            async: false,
            data: ({ studentId: studentId }),
            success: function () {
                GetLecturerPracticeDetails('@Model.Id');
            }
        });
    }
</script>

@foreach(var item in Model.Levels)
{
    <div style="border: 1px solid gray; margin: 5px; padding: 5px;">
        <div class="largeText"><b>@item.Name</b></div>
        <div>Max students:
            <input id="lecturer_Loading_@(item.Id)" type="text" value="@item.MaxValue" style="width: 25px;"/>
            <input type="button" value="Update" onclick="UpdateLecturerLoadingForCompany('@Model.Id.ToString()', '@item.Id'); return false;"/>
        </div>
        <div>
            <ul>
                @foreach(var company in item.Companies)
                {
                    <li>@company.Name (@company.CurrentStudents/@company.MaxStudents)
                        <img src="@Url.Content("~/Content/Icons/delete.png")" class="mediumIcon" onclick="DeleteCompanyFromLecturer('@Model.Id.ToString()', '@item.Id', '@company.Id'); return false;"/>
                        @if (company.CurrentStudents > company.MaxStudents)
                        {
                            <div style="color: red;">Limit exceeded</div>
                        }
                    </li>
                }
            </ul>
            <div style="margin-top: 20px">Total students: @item.Companies.Sum(r => r.CurrentStudents)</div>
            @if (item.MaxValue < item.Companies.Sum(r => r.CurrentStudents))
            {
                <div style="color: red;">Limit exceeded</div>
            }
        </div>
        <div>
            @Html.DropDownList(String.Format("selectListCompanies_level_{0}", item.Id), item.SelectListCompanies)
            <input type="button" value="Add" onclick="AddCompanyForLecturer('@Model.Id.ToString()', '@item.Id'); return false;"/>
        </div>
    </div>
}

<div style="border: 1px solid gray; margin: 5px; padding: 5px;">
    <div class="largeText"><b>Magisters</b></div>
    @foreach (var item in Model.Students)
    {
        <div><b>@(String.Format("{0} {1}", item.GroupName, item.FIO))</b> <img src="@Url.Content("~/Content/Icons/delete.png")" class="mediumIcon" onclick="DeleteStudentFromLecturer('@item.Id'); return false;"/></div>
    }
    <div>
        @Html.DropDownList("selectListStudents", Model.SelectListStudents)
        <input type="button" value="Add" onclick="AddStudentForLecturer('@Model.Id.ToString()'); return false;"/>
    </div>
</div>